<template>
  <div class="w-card content-box">
    <div class="titel-box">
      <span class="title">佣金订单</span>
      <el-button style="margin-left: auto;" @click="exportHandle(1)">导出</el-button>
    </div>
    <div class="my-search-box">
      <div>
        <span>佣金订单：</span>
        <el-input v-model="searchForm.incomeOrderId" clearable placeholder="请输入佣金订单ID" />
      </div>
      <div>
        <span>业务标识：</span>
        <el-input v-model="searchForm.businessOrderId" clearable placeholder="请输入业务标识ID" />
      </div>
      <div>
        <span>代理商：</span>
        <el-input v-model="searchForm.shopName" clearable placeholder="请输入代理商" />
      </div>
      <div>
        <span>订单类型：</span>
        <el-select v-model="searchForm.orderType" clearable>
          <el-option v-for="item in orderTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </div>
    </div>
    <div class="my-search-box">
      <div class="datebox">
        <span>时间段：</span>
        <el-date-picker v-model="searchForm.date" type="datetimerange" value-format="x" start-placeholder="开始"
          end-placeholder="结束" />
      </div>
      <div class="f-ml-20">
        <el-button @click="searchHandle" type="primary">查询</el-button>
        <el-button @click="resetHandle">重置</el-button>
      </div>
    </div>
    <div class="table-box">
      <el-table :max-height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
        :cell-style="{fontSize:'14px',color:'#7b7b7d'}" :data="tableData" stripe @selection-change="handleSelectionChange">
        <el-table-column type="index" width="55" label="序号" align="center" />
        <el-table-column type="selection" width="55" />
        <el-table-column prop="incomeOrderId" label="佣金订单" align="center">
          <template v-slot="scope">
            {{ scope.row.incomeOrderId || '' }}
          </template>
        </el-table-column>
        <el-table-column prop="businessOrderId" label="业务标识" align="center">
          <template v-slot="scope">
            {{ scope.row.businessOrderId || '' }}
          </template>
        </el-table-column>
        <el-table-column prop="shopName" label="代理商" align="center">
          <template v-slot="scope">
            {{ scope.row.shopName || '' }}
          </template>
        </el-table-column>
        <el-table-column prop="orderType" label="订单类型" align="center">
          <template v-slot="scope">
            {{ getLabel(orderTypeOptions,scope.row?.orderType) }}
          </template>
        </el-table-column>
        <el-table-column prop="incomeName" label="佣金名称" align="center">
          <template v-slot="scope">
            {{ scope.row.incomeName || '' }}
          </template>
        </el-table-column>
        <el-table-column prop="income" label="佣金金额(元)" align="center">
          <template v-slot="scope">
            {{ scope.row.income || '' }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="分佣时间" align="center">
          <template v-slot="scope">
            {{ scope.row.createTime ? getDateTime(scope.row.createTime)[6] : '' }}
          </template>
        </el-table-column>
      </el-table>
      <div class="my-table-footer">
        <div>
          <el-button :disabled="chooseList.length == 0" @click="exportHandle(2)">批量导出</el-button>
        </div>
        <el-pagination style="margin-left:auto;" v-model:currentPage="page" v-model:page-size="size"
          :page-sizes="[10, 20, 50, 100]" small :background="true" layout="total, sizes, prev, pager, next, jumper" :total="total"
          @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>

<script>
import { getWindowWidthAndHeight, warningDeal, getDateTime, BlobDownLoad } from '@/utils/utils'
import { onMounted, reactive, toRefs } from 'vue'
import api from '../api'
import { useRoute, useRouter } from 'vue-router'
export default {
  setup() {
    const route = useRoute()
    const router = useRouter()

    const state = reactive({
      tableHeight: 0,
      page: 1,
      size: 10,
      total: 0,
      tableData: [],
      chooseList: [],
      searchForm: {
        timeType: 1
      },

      orderTypeOptions: [
        {
          value: '_hkb',
          label: '售卡佣金'
        },
        {
          value: '_tuijian',
          label: '同级奖励'
        },
        {
          value: '_xufeib',
          label: '直营日结'
        },
        {
          value: '_xufeic',
          label: '直营月结'
        },
        {
          value: '_xufeiteamc',
          label: '团队月结'
        },
        {
          value: '_fenhong1',
          label: '分红一'
        },
        {
          value: '_fenhong2',
          label: '分红二'
        },
        {
          value: '_fenhong3',
          label: '分红三'
        },
        {
          value: '_fenhong4',
          label: '分红四'
        },
      ],
    })

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
        ...state.searchForm,
        timeMin: (state.searchForm.date && state.searchForm.date[0]) ? state.searchForm.date[0] : null,
        timeMax: (state.searchForm.date && state.searchForm.date[1]) ? state.searchForm.date[1] : null,
        orderBy: 'id desc',
      }
      api.incomeOrderList(obj).then(res => {
        if (res.code == '200') {
          state.tableData = res.data.list
          state.total = res.data.total
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function searchHandle() {
      state.page = 1
      getTableData()
    }

    function resetHandle() {
      state.searchForm = {
        timeType: 1
      }
      searchHandle()
    }

    function handleSelectionChange(e) {
      state.chooseList = e
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    function exportHandle(type) {
      let obj = {}
      switch (type) {
        case 1:
          obj = {
            page: state.page,
            size: state.size,
            ...state.searchForm,
            timeMin: (state.searchForm.date && state.searchForm.date[0]) ? state.searchForm.date[0] : null,
            timeMax: (state.searchForm.date && state.searchForm.date[0]) ? state.searchForm.date[0] : null,
          }
          break
        case 2:
          obj = {
            page: state.page,
            size: state.size,
            incomeOrderId: getSysOrderId(state.chooseList).join(','),
          }
          break
      }
      api.incomeOrderDown({ ...obj, responseType: "blob" }).then(res => {
        const _fileName = decodeURIComponent(res.headers['content-disposition'].split(';')[1].split('=')[1])
        BlobDownLoad(res, _fileName)
      }).catch(err => {
        console.log(err)
      })
    }

    function getSysOrderId(list) {
      const myArr = []
      list && list.map(item => {
        myArr.push(item.incomeOrderId)
      })
      return myArr
    }

    function getLabel(list, value) {
      let label = ''
      list && value && list.map(item => {
        if (item.value == value) {
          label = item.label
        }
      })
      return label
    }

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 330
      state.searchForm.shopName = route.query.shopName || null
      getTableData()
    })

    return {
      ...toRefs(state),
      searchHandle,
      resetHandle,
      getDateTime,
      handleSelectionChange,
      handleCurrentChange,
      handleSizeChange,
      exportHandle,
      getLabel,
    }
  }
}
</script>

<style lang="scss" scoped>
.content-box {
  height: 100%;
  overflow: hidden;
  overflow-y: auto;
  box-sizing: border-box;
  padding: 20px;
  .titel-box {
    display: flex;
    align-items: center;
    .title {
      font-size: 16px;
      font-weight: bold;
    }
  }
  .my-search-box {
    box-sizing: border-box;
    padding: 20px 20px 20px 0;
    padding-bottom: 0;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 10px;

    > div {
      width: 350px;
      display: flex;
      align-items: center;

      > span {
        flex-shrink: 0;
        width: 120px;
        text-align: right;
      }
    }

    .datebox {
      width: 500px;
    }
  }
  .table-box {
    margin-top: 10px;
    .my-table-footer {
      display: flex;
      align-items: center;
      margin-top: 10px;
      flex-wrap: wrap;
      gap: 10px;
    }
  }
}
.oStatus {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  &::before {
    content: "";
    display: block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
  }
}
.oStatus1 {
  &::before {
    background-color: #f04134;
  }
}
.oStatus2 {
  &::before {
    background-color: #00a854;
  }
}
.oStatus3 {
  &::before {
    background-color: #b6b4b4;
  }
}
.oStatus4 {
  &::before {
    background-color: #00a854;
  }
}
</style>